package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.jaina.utils.CitizenInfoDomainConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.citizenproperty.CitizenPropertyDTO;
import com.guldan.jaina.dto.citizenproperty.CitizenPropertySearchDTO;
import com.guldan.mapper.db.entity.CitizenProperty;
import com.guldan.mapper.db.entity.CitizenPropertyExample;
import com.guldan.mapper.db.mapper.CitizenPropertyMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class CitizenPropertyManager extends BaseManager {

    @Autowired
    private CitizenPropertyMapperExt citizenPropertyMapperExt;

    //获取单个居民属性信息
    public CitizenPropertyDTO get(Integer citizenPropertyId) {
        return CitizenInfoDomainConvertUtils.convert(citizenPropertyMapperExt.selectByPrimaryKey(citizenPropertyId));
    }

    //获取单个居民属性信息，通过 citizen_info_id
    public CitizenPropertyDTO getByCitizenInfoId(Integer citizenInfoId) {

        CitizenPropertySearchDTO search = new CitizenPropertySearchDTO();
        search.setCitizenInfoId(citizenInfoId);
        List<CitizenProperty> list = citizenPropertyMapperExt.selectByExample(buildSearchExample(search));

        CitizenPropertyDTO dto = new CitizenPropertyDTO();
        if (list != null && list.size() > 0) {
            dto = CitizenInfoDomainConvertUtils.convert(list.get(0));
        }
        return dto;
    }

    //新增/更新单个居民属性，传入：CitizenPropertyDTO
    public CitizenPropertyDTO save(CitizenPropertyDTO dto) {

        CitizenProperty entity;
        if (dto.getCitizenPropertyId() != null && dto.getCitizenPropertyId() > 0) {
            entity = citizenPropertyMapperExt.selectByPrimaryKey(dto.getCitizenPropertyId());

            entity.setCitizenPropertyId(dto.getCitizenPropertyId());
            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setIsOrphan(dto.getOrphan());
            entity.setOrphanTypeCode(dto.getOrphanTypeCode());
            entity.setIsUniqChild(dto.getUniqChild());
            entity.setIsHead(dto.getHead());
            entity.setIsDeform(dto.getDeform());
            entity.setIsArmyReturn(dto.getArmyReturn());
            entity.setIsOld(dto.getOld());
            entity.setIsSpecial(dto.getSpecial());
            entity.setIsReservoirsim(dto.getReservoirsim());

            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(entity.getModifier());

            citizenPropertyMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new CitizenProperty();

            entity.setCitizenPropertyId(dto.getCitizenPropertyId());
            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setIsOrphan(dto.getOrphan());
            entity.setOrphanTypeCode(dto.getOrphanTypeCode());
            entity.setIsUniqChild(dto.getUniqChild());
            entity.setIsHead(dto.getHead());
            entity.setIsDeform(dto.getDeform());
            entity.setIsArmyReturn(dto.getArmyReturn());
            entity.setIsOld(dto.getOld());
            entity.setIsSpecial(dto.getSpecial());
            entity.setIsReservoirsim(dto.getReservoirsim());


            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            citizenPropertyMapperExt.insertSelective(entity);
        }
        dto = CitizenInfoDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个居民属性信息
    public Boolean delete(Integer citizenPropertyId) {
        CitizenProperty entity = citizenPropertyMapperExt.selectByPrimaryKey(citizenPropertyId);
        entity.setIsDelete(true);
        return citizenPropertyMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //分页查询居民属性信息列表
    public Pagination<CitizenPropertyDTO> list(CitizenPropertySearchDTO citizenPropertySearchDTO) {
        Pagination<CitizenPropertyDTO> result = new Pagination<CitizenPropertyDTO>(citizenPropertySearchDTO.getCurrentPage(), citizenPropertySearchDTO.getPerPage());
        CitizenPropertyExample example = buildSearchExample(citizenPropertySearchDTO);
        int total = citizenPropertyMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<CitizenPropertyDTO> data = new ArrayList<CitizenPropertyDTO>();
            List<CitizenProperty> list = citizenPropertyMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (CitizenProperty entity : list) {
                CitizenPropertyDTO dto = CitizenInfoDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    //build查询Example
    private CitizenPropertyExample buildSearchExample(CitizenPropertySearchDTO search) {
        CitizenPropertyExample example = new CitizenPropertyExample();
        CitizenPropertyExample.Criteria criteria = example.createCriteria();

        if (search.getCitizenPropertyId() != null && search.getCitizenPropertyId() > 0) {
            criteria.andCitizenPropertyIdEqualTo(search.getCitizenPropertyId());
        }
        if (search.getCitizenInfoId() != null && search.getCitizenInfoId() > 0) {
            criteria.andCitizenInfoIdEqualTo(search.getCitizenInfoId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("citizen_property_id asc");
        }
        return example;
    }

}
